package life.mahu.community.controller;

import com.github.pagehelper.PageInfo;
import life.mahu.community.dto.PageResultDTO;
import life.mahu.community.dto.ResultDTO;
import life.mahu.community.model.Tag;
import life.mahu.community.service.CategoryService;
import life.mahu.community.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Tag 后台数据控制层
 */
@Controller
@Slf4j
public class AdminTagController {

    @Autowired
    private CategoryService categoryService;

    @Resource
    private RedisUtil redis;

    @GetMapping("/admin/tag/list")
    public String getListHtml(){
        return "/admin/tag/list";
    }
    @GetMapping("/admin/tag/edit")
    public String getEditHtml(Model model,  @RequestParam("id") String id){
        model.addAttribute("tagId", id);
        return "/admin/tag/edit";
    }
    @GetMapping("/admin/tag/detail")
    public String getDetailHtml(Model model,  @RequestParam("id") String id){
        model.addAttribute("tagId", id);
        return "/admin/tag/detail";
    }

    @GetMapping("/admin/tag/add")
    public String getAddHtml(){
        return "/admin/tag/edit";
    }

    @ResponseBody
    @GetMapping("/admin/tag/delTag")
    public ResultDTO delUser(Model model,  @RequestParam("id") Integer id){
        //从服务端查询数据
        Boolean success = categoryService.delTagById(id);
        if(success){
            return ResultDTO.okOf(200, "删除成功！");
        }else{
            return ResultDTO.okOf(201, "查无数据！");
        }
    }

    @ResponseBody
    @GetMapping("/admin/tag/getList")
    public PageResultDTO getUserList(@RequestParam("id") String id,
                                 @RequestParam("name") String name,
                                 @RequestParam("categoryId") String categoryId,
                                 @RequestParam("limit") Integer limit,
                                 @RequestParam("page") Integer page){

        Tag tag = new Tag();
        if(!("".equals(id) || id==null)){
            tag.setId(Integer.parseInt(id));
        }
        if(!("".equals(name) || name==null)){
            tag.setName(name);
        }
        if(!("".equals(categoryId) || categoryId==null)){
            tag.setCategoryId(Integer.parseInt(categoryId));
        }

        //List<User> userList = userService.getUserList(user);
        PageInfo tagListPage = categoryService.getTagListPage(tag, page, limit);

        return PageResultDTO.okOf(0, "查询成功！", tagListPage.getTotal(), tagListPage.getList());
    }

    @ResponseBody
    @PostMapping("/admin/tag/searchTagDetail")
    public ResultDTO getUserDetail(@RequestParam("id") Integer id){

        Tag tag =  categoryService.getTagById(id);

        return ResultDTO.okOf(tag);

    }


    @ResponseBody
    @PostMapping("/admin/tag/saveTag")
    public ResultDTO saveUser(HttpServletRequest request, HttpServletResponse response){

        ResultDTO resultDTO = new ResultDTO();
        try {
            Map<String, String[]> map = request.getParameterMap();
            Tag tag = new Tag();
            BeanUtils.populate(tag, map);
            Boolean success = false;
            if(tag.getId() == 0 || tag.getId() == null){
                success = categoryService.insertTag(tag);
            }else{
                success = categoryService.updateTagById(tag);
            }
            if(success){
                redis.del("mahuTag");
                resultDTO.setCode(200);
                resultDTO.setMessage("保存成功！");
            }else{
                resultDTO.setCode(100);
                resultDTO.setMessage("系统繁忙！");
            }

        } catch (Exception e) {
            e.printStackTrace();
            resultDTO.setCode(101);
            resultDTO.setMessage("服务器出错了！");
        }

        return resultDTO;

    }

}
